package com.lwl.Algorithmic_data_structure.class13贪心算法;

import com.lwl.Utils.MyNode;

/**
 * @author lwl
 * @Description 给出一个多叉树，每个节点都有一个快乐值，你需要将一个树的快乐值收集起来，但是连续的父子节点不可同时收集，求收集的最大快乐值
 * @date 2023/6/1 10:18
 */
public class Code01getMaxHappy {

    class Info {

        int comeHappy;

        int notComeHappy;

        public Info(int comeHappy, int notComeHappy) {
            this.comeHappy = comeHappy;
            this.notComeHappy = notComeHappy;
        }
    }

    public Info getMaxHappy(MyNode head) {
        if (head == null) {
            return new Info(0, 0);
        }
        int comeHappy = head.value;
        int notComeHappy = 0;
        for (MyNode node : head.subNodes) {
            Info info = getMaxHappy(node);
            comeHappy += info.notComeHappy;
            notComeHappy += Math.max(info.comeHappy, info.notComeHappy);
        }
        return new Info(comeHappy, notComeHappy);
    }

}
